package com.youyu.core.jms.kafka.plugin;

import com.youyu.core.jms.kafka.IConsumer;
import org.yx.bean.Bean;
import org.yx.bean.IOC;
import org.yx.bean.Plugin;
import org.yx.log.Log;
import org.yx.util.CollectionUtils;

import java.util.List;

/**
 * <p>Author:  dxj</p>
 * <p>Date:2017/8/28 14:26</p>
 * <p>Description:kafka消费者线程启动</p>
 * <p>Copyright: Copyright (c) 2017</p>
 * <p>Company: youyu</p>
 */
@Bean
public class KafkaConsumerPlugin implements Plugin {
    @Override
    public void start() {
        List list = IOC.getBeans(IConsumer.class);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(thread -> {
                Log.get(KafkaConsumerPlugin.class).info("消费者"+((IConsumer)thread).getConsumerName()+"启动开始...");
                ((Thread)thread).start();
                Log.get(KafkaConsumerPlugin.class).info("消费者"+((IConsumer)thread).getConsumerName()+"启动完成...");
            });
        }
    }

    @Override
    public void stop() {
        List list = IOC.getBeans(IConsumer.class);
        if(CollectionUtils.isNotEmpty(list)){
            list.forEach(thread -> {
                Thread thread1 = (Thread)thread;
                if(thread1 != null && thread1.isAlive()){
                    thread1.interrupt();
                }
            });
        }
    }
}
